package com.atguigu.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.mapper.WechatMenuMapper;
import com.atguigu.model.wechat.Menu;
import com.atguigu.service.WechatMenuService;
import com.atguigu.vo.wechat.MenuVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class WechatMenuServiceImpl extends ServiceImpl<WechatMenuMapper, Menu> implements WechatMenuService {
    @Autowired
    private WechatMenuMapper wechatMenuMapper;
    @Autowired
    private WxMpService wxMpService;
    @Override
    public List<MenuVo> findMenuInfo() {
        List<Menu> menus = wechatMenuMapper.selectList(null);
        List<MenuVo> menuVoParentList = menus.stream().filter(menu -> menu.getParentId().intValue() == 0).collect(Collectors.toList()).stream().map(new Function<Menu, MenuVo>() {
            @Override
            public MenuVo apply(Menu menu) {
                MenuVo menuVo = new MenuVo();
                BeanUtils.copyProperties(menu, menuVo);
                return menuVo;
            }
        }).collect(Collectors.toList());
        for (MenuVo menuParent : menuVoParentList) {
            List<MenuVo> children=new ArrayList<>();
            for (Menu menuSon : menus) {
                if (menuSon.getParentId().intValue()==menuParent.getId().intValue()){
                    MenuVo menuVo = new MenuVo();
                    BeanUtils.copyProperties(menuSon,menuVo);
                    children.add(menuVo);
                }
            }
            menuParent.setChildren(children);
        }
        return menuVoParentList;
    }

    @Override
    public void syncMenu() {
        List<MenuVo> menuInfo = this.findMenuInfo();
        JSONArray jsonArray=new JSONArray();
        for (MenuVo menuVo : menuInfo) {
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("name",menuVo.getName());
            jsonObject.put("type",menuVo.getType());
            if (!CollectionUtils.isEmpty(menuVo.getChildren())){
                JSONArray jsonArray1=new JSONArray();
                for (MenuVo child : menuVo.getChildren()) {
                    JSONObject jsonObject1 = new JSONObject();
                    jsonObject1.put("name",child.getName());
                    jsonObject1.put("type",child.getType());
                    if (child.getType().equals("view")){
                        jsonObject1.put("url","http://sdps.v2.idcfengye.com/#"+child.getUrl());
                    }else{
                    jsonObject1.put("key",child.getMeunKey());
                    }
                    jsonArray1.add(jsonObject1);
                }

             jsonObject.put( "sub_button",jsonArray1);
            }else{
                jsonObject.put("url","http://sdps.v2.idcfengye.com/#"+menuVo.getUrl());
            }
            jsonArray.add(jsonObject);

        }
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("button",jsonArray);
        try {
            wxMpService.getMenuService().menuCreate(jsonObject.toJSONString());
        } catch (WxErrorException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void removeMenu() {
        try {
            wxMpService.getMenuService().menuDelete();
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
    }
}
